/**
*
* @file sonar.h
*
* @brief Header file for sonar definitions and function prototypes
*
* @n @n CSc 460/560 Design and Analysis of Real Time Systems - Fall 2007
*
* @author Leanne Ross
* @author Clecio Varjao
*
*/

#ifndef __SONAR_H__
#define __SONAR_H__


#define SONAR_DDR               DDRC						    /** data direction register of port C **/
#define SONAR_PORT				PORTC

#define SONAR_PORTNUM_ECHO		PORTC7
#define SONAR_ECHO_MASK   		(_BV(7))					    /** set Bit 7 **/

#define SONAR_PORTNUM_PULSE		PORTC6
#define SONAR_PULSE_MASK   		(_BV(6))					    /** set Bit 6 **/

#define SET_RISING_EDGE()	(TCCR3B |= _BV(ICES3)) 				/** set input capture to detect rising edge **/
#define SET_FALLING_EDGE()	(TCCR3B &= ~(_BV(ICES3)))			/** set input capture to detect falling edge **/
#define IS_RISING_EDGE()	(TCCR3B & _BV(ICES3))				/** check if rising edge on input capture set **/
#define IS_FALLING_EDGE()	~(TCCR3B & _BV(ICES3))				/** check if falling edge on input capture set **/

#define SET_IC_ENABLE()		(TIMSK3 |= _BV(ICIE3))				/**	set input capture interrupt enable **/
#define CLEAR_IC_FLAG()		(TIFR3 |= _BV(ICF3))				/** clear input capture flag **/

#define ENABLE_ECHO()		(SONAR_PORT |= SONAR_ECHO_MASK)		/**	enable input capture on Port C, pin 7 **/
#define DISABLE_ECHO()		(SONAR_PORT &= ~(SONAR_ECHO_MASK))	/** disable input capture on Port C, pin 7 **/
#define ENABLE_PULSE()		(SONAR_PORT |= SONAR_PULSE_MASK)	/** enable output capture on Port C, pin 6 **/
#define DISABLE_PULSE()		(SONAR_PORT &= ~(SONAR_PULSE_MASK))	/** disable output capture on Port C, pin 6 **/

#define US_PER_INCH			147  								/** Microseconds per inch **/

void sonar_init();
void trigger_sonar();

uint8_t read_distance();

#endif //__SONAR_H__
